package com.haixiaoke.flat.lock.controller;

import java.util.List;

import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.lock.domain.Group;
import com.haixiaoke.saas.lock.service.IGroupService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.token.FrontTokenComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 分组Controller
 * 
 * @author panda
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/front/flat/lock/group")
public class FlatGroupController extends BaseController {
    @Autowired
    private IGroupService groupService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    /**
     * 查询分组列表
     */
    @AuthCheck
    @GetMapping("/list")
    public AjaxResult list(Group group) {
        group.setUserId(frontTokenComponent.getUserIdNotNull());
        List<Group> list = groupService.selectGroupList(group);
        return success(list);
    }

    /**
     * 新增分组
     */
    @AuthCheck
    @PostMapping
    public AjaxResult add(@RequestBody Group group) {
        group.setCreateBy(frontTokenComponent.getLoginUser().getUser().getPhone());
        group.setUserId(frontTokenComponent.getUserIdNotNull());
        return groupService.insertGroup(group);
    }

    /**
     * 修改分组
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Group group) {
        if (group.getGroupId() == null) {
            return error("分组ID不能为空");
        }
        group.setUpdateBy(frontTokenComponent.getLoginUser().getUser().getPhone());
        return groupService.updateGroup(group, frontTokenComponent.getUserIdNotNull());
    }

    /**
     * 移动分组
     * @param group
     * @return
     */
    @PutMapping("/move")
    public AjaxResult move(@RequestBody Group group) {
        return groupService.moveGroup(group);
    }

    /**
     * 删除分组
     */
    @DeleteMapping("/{groupId}")
    public AjaxResult remove(@PathVariable Long groupId) {
        return groupService.deleteGroupByGroupId(groupId, frontTokenComponent.getUserIdNotNull());
    }
}
